package offer;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 剑指 Offer 55 - I. 二叉树的深度
 * </p>
 *
 * @author xiaozuolin
 * @date 2021/9/28 17:34
 */
public class Offer55 {

    public int maxDepth(TreeNode root) {
        return dfs(root);
    }

    //后序遍历
    public int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(dfs(root.left), dfs(root.right)) + 1;
    }

    //层序遍历
    public int bfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        List<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        List<TreeNode> tmp;
        int res = 0;
        while (!queue.isEmpty()) {
            tmp = new LinkedList<>();
            for (TreeNode node : queue) {
                if (node.left != null) {
                    tmp.add(node.left);
                }
                if (node.right != null) {
                    tmp.add(node.right);
                }
            }
            queue = tmp;
            res++;
        }
        return res;
    }
}
